﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
public interface IMath
{
    int Add(int a, int b);
    int Subtract(int a, int b);
    int Multiply(int a, int b);
    int Divide(int a, int b);
}
public class Math : IMath
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Subtract(int a, int b)
    {
        return a - b;
    }

    public int Multiply(int a, int b)
    {
        return a * b;
    }

    public int Divide(int a, int b)
    {
        if (b != 0)
        {
            return a / b;
        }
        else
        {
            throw new DivideByZeroException("Cannot divide by zero.");
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        // 使用反射加载 Math 类库
        Assembly mathAssembly = Assembly.LoadFrom("Math.dll");
        Type mathType = mathAssembly.GetType("Math.Math");

        // 创建 Math 类的实例
        object mathInstance = Activator.CreateInstance(mathType);

        // 获取 IMath 接口
        Type mathInterfaceType = typeof(IMath);

        // 使用反射调用接口中的方法
        MethodInfo addMethod = mathInterfaceType.GetMethod("Add");
        int resultAdd = (int)addMethod.Invoke(mathInstance, new object[] { 5, 3 });

        MethodInfo subtractMethod = mathInterfaceType.GetMethod("Subtract");
        int resultSubtract = (int)subtractMethod.Invoke(mathInstance, new object[] { 5, 3 });

        MethodInfo multiplyMethod = mathInterfaceType.GetMethod("Multiply");
        int resultMultiply = (int)multiplyMethod.Invoke(mathInstance, new object[] { 5, 3 });

        MethodInfo divideMethod = mathInterfaceType.GetMethod("Divide");
        int resultDivide = (int)divideMethod.Invoke(mathInstance, new object[] { 15, 3 });

        // 输出结果
        Console.WriteLine("Addition: " + resultAdd);
        Console.WriteLine("Subtraction: " + resultSubtract);
        Console.WriteLine("Multiplication: " + resultMultiply);
        Console.WriteLine("Division: " + resultDivide);

        Console.ReadLine();
    }
}
